package org.mics.file.service.impl;

import java.io.File;
import java.io.IOException;

import javax.annotation.Resource;

import org.mics.file.bean.FileInfo;
import org.mics.file.entity.FileDO;
import org.mics.file.exception.FileUploadException;
import org.mics.file.repository.FileRepository;
import org.mics.file.service.FileService;
import org.mics.file.vo.FileVO;
import org.mics.lang.bean.BeanConvertUtil;
import org.mics.lang.calculate.UUIDUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件服务
 * @author mics
 * @date 2020年7月2日
 * @version  1.0
 */
@Service
public class FileServiceImpl implements FileService{
	private static final Logger LOGGER = LoggerFactory.getLogger(FileServiceImpl.class);
	@Resource
	private FileRepository fileRepository ;
	
	@Value("${cyy.plugin.file.filePrefixPath}")
	private String  filePrefixPath;
	
	@Override
	public FileVO upload(MultipartFile file,String service) {
		LOGGER.info("文件上传-开始");
		 // 保存到硬盘
		FileInfo fileInfo = saveFileToDisk(file,service);
		// 保存相关数据到数据库
		FileVO fileVO =  saveFile(fileInfo);
		LOGGER.debug("文件上传-service-结束：[fileVO:{}]", fileVO);
		 return fileVO;
	}
	
	private FileVO saveFile(FileInfo fileInfo) {
		  FileDO  fileDO = BeanConvertUtil.convert(fileInfo, FileDO.class);
		  fileDO =  fileRepository.save(fileDO);
		  return BeanConvertUtil.convert(fileDO, FileVO.class);
	}
	  
	private FileInfo saveFileToDisk(MultipartFile file,String service) {
		  //文件信息 7
		  FileInfo fileInfo = getInfo(file,service);
		  //保存文件 
		  File finalPath = new File(fileInfo.getStorePath()); 
		  //保存文件到磁盘 
		  try { 
			  file.transferTo(finalPath);
		  } catch (IOException e) {
			  LOGGER.error("保存文件异常 finalPathString:{}", fileInfo.getStorePath()); 
			  throw new FileUploadException("保存文件异常",e);
		  } 
		  return fileInfo;
	  }
	 
	  private FileInfo getInfo(MultipartFile file,String service) { 
		  //文件原始参数 
		  FileInfo fileInfo = new FileInfo();
		  String originalFilename = file.getOriginalFilename(); 		  
		  String ext = originalFilename.substring(originalFilename.lastIndexOf(".")+1); 
		  fileInfo.setFileType(ext);
		  
		  Long size = file.getSize();
		  fileInfo.setSize(size);
		  
		  //文件存储参数 
		  String storeName = UUIDUtil.randomUuId(); 
		  fileInfo.setName(storeName+"."+ext);
		  
		  String folderName = service;
			
		 String folderPath = filePrefixPath+folderName; 
		 File folder = new  File(folderPath);
		 if(!folder.exists()) { 
			 folder.mkdirs(); 
		 }
			 
		 String filePath = filePrefixPath+File.separator+folderName + File.separator +storeName+"."+ext; 
		 fileInfo.setStorePath(filePath);
		 
		 String accessPath = folderName + File.separator +storeName+"."+ext;
		 fileInfo.setAccessPath(accessPath);
		 return fileInfo;
	  }
	 
	@Override
	public String upload2OSS(MultipartFile file) {
		return null;
	}

	@Override
	public FileDO findById(String id) {
		return fileRepository.getById(id);
	}


}
